// 实体基础模型 - 所有战斗实体的数据模型基类
export abstract class EntityModel {
    public id: number;                      // 唯一ID
    public configId: number;                // 配置ID
    public category: EntityCategory;        // 实体类别
    public position: cc.Vec2 = cc.v2(0, 0); // 位置
    public direction: cc.Vec2 = cc.v2(1, 0);// 朝向
    public attributes: AttributeModel;      // 属性
    public isDead: boolean = false;         // 是否死亡
    
    constructor(configId: number) {
        this.configId = configId;
        this.id = EntityIdGenerator.generateId();
        this.attributes = new AttributeModel();
    }
    
    // 初始化属性
    public initAttributes(config: any): void {
        this.attributes.initFromConfig(config);
    }
    
    // 获取当前生命值
    public getHealth(): number {
        return this.attributes.getValue(AttributeType.HP);
    }
    
    // 获取最大生命值
    public getMaxHealth(): number {
        return this.attributes.getMaxValue(AttributeType.HP);
    }
    
    // 受到伤害
    public takeDamage(damage: number, source: EntityModel): number {
        // 计算实际伤害
        const actualDamage = this.calculateDamage(damage, source);
        
        // 减少生命值
        const currentHealth = this.getHealth();
        const newHealth = Math.max(0, currentHealth - actualDamage);
        this.attributes.setValue(AttributeType.HP, newHealth);
        
        // 检查死亡
        if (newHealth <= 0 && !this.isDead) {
            this.isDead = true;
        }
        
        return actualDamage;
    }
    
    // 计算伤害
    protected calculateDamage(damage: number, source: EntityModel): number {
        // 基础伤害计算公式
        const defense = this.attributes.getValue(AttributeType.DEFENSE);
        let actualDamage = damage * (1000 / (1000 + defense));
        
        // 可以在子类中重写，添加更复杂的伤害计算逻辑
        return Math.max(1, Math.floor(actualDamage));
    }
    
    // 治疗
    public heal(amount: number): number {
        const currentHealth = this.getHealth();
        const maxHealth = this.getMaxHealth();
        const newHealth = Math.min(maxHealth, currentHealth + amount);
        this.attributes.setValue(AttributeType.HP, newHealth);
        
        return newHealth - currentHealth;
    }
    
    // 复活
    public revive(healthPercent: number = 1.0): void {
        if (this.isDead) {
            this.isDead = false;
            const maxHealth = this.getMaxHealth();
            this.attributes.setValue(AttributeType.HP, maxHealth * healthPercent);
        }
    }
    
    // 更新
    public update(dt: number): void {
        // 更新属性
        this.attributes.update(dt);
    }
}

// 实体ID生成器
class EntityIdGenerator {
    private static nextId: number = 1;
    
    public static generateId(): number {
        return this.nextId++;
    }
    
    public static reset(): void {
        this.nextId = 1;
    }
}

// 实体类别枚举
export enum EntityCategory {
    HERO,
    MONSTER,
    TOWER,
    ELF,
    PET,
    PROJECTILE
}